package dao.lmpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import dao.BaseDao;
import dao.Studentdao;
import entity.Student;

public class StudentDaolmpl extends BaseDao implements Studentdao {

	public Student queryStudentById(int id) throws Exception {
		Connection conn = getConnection();
		String sql="SELECT * FROM student WHERE id = ?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, id);
		ResultSet result = pstmt.executeQuery();
		
		Student student = null;
		if (result.next()) {
			student = new Student(
					result.getInt("id"),
					result.getString("name"),
					result.getString("sex"),
					result.getInt("age"),
					result.getString("major"),
					result.getInt("grade"),
					result.getString("address"),
					result.getString("phone"),
					result.getString("memo"),
					result.getString("password"),
					result.getString("dormnum"),
					result.getString("bednum"),
					result.getString("award"),
					result.getString("punish")
					);
		}
		
		
		return student;
	}

	@Override
	public List<Student> queryStudents() throws Exception {
		Connection conn = getConnection();
		
		String preparedSql = "select * from student;";
		
		PreparedStatement pstmt = conn.prepareStatement(preparedSql);
		
		ResultSet result = pstmt.executeQuery();
		
		List<Student> students =new ArrayList<Student>();
		
		while (result.next()) {
			 Student student = new Student(result.getInt("id"),
					result.getString("name"),
					result.getString("sex"),
					result.getInt("age"),
					result.getString("major"),
					result.getInt("grade"),
					result.getString("address"),
					result.getString("phone"),
					result.getString("memo"),
					result.getString("password"),
					result.getString("dormnum"),
					result.getString("bednum"),
					result.getString("award"),
					result.getString("punish")
					);
			students.add(student);
		}

		return students;
	}

	@Override
	public List<Student> queryStudentByName(String name) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int insertStudent(Student student) throws Exception {
		 
		return 0;
	}

	@Override
	public int updateStudent(Student student) throws Exception {
		String sql ="UPDATE `student`  SET `name` = ?, `sex` = ?, `age` = ?, `major` = ?, `password` = ? WHERE `id`= ?;";
		// TODO Auto-generated method stub
		List<Object> param = new ArrayList<Object>();
		param.add(student.getName());
		param.add(student.getSex());
		param.add(student.getAge());
		param.add(student.getMajor());
		param.add(student.getPassword());
		param.add(student.getId());
		int rows = exceuteInsert(sql, param);
		
		return rows;
	}
	public int deleteStudentByName(String Name) throws Exception {
        return 0;
    }


}
